#include<iostream>
#include<stdio.h>
using namespace std;

float calculate_exp(float base, int exp)
{
    if (exp == 0) return 1;
    if (exp == 1) return base;
    float temp = calculate_exp(base, exp / 2);
    if (exp % 2 == 0) return temp * temp;
    return temp * temp * base;
}

float atof_david(const string& str)
{
    int base = 0, exp = 0;
    float float_part = 0.0;
    float float_exp = 1.0;
    int status = 0;
    int positive = 1;
    for (size_t i = 0;i < str.length(); ++i)
    {
        char c = str[i];
        switch (status)
        {
            case 0:
                if (c == '+') positive = 1;
                else if (c == '-') positive = -1;
                else if (c == 'e') 
                {
                    positive = 1;
                    status = 3;
                }
                else if (c >= '0' && c <= '9') base = base * 10 + positive * (c - '0');
                else if (c == '.')
                {
                    status = 2;
                }
                else {
                    cout<< "wrong format" << endl;
                    return 0.0;
                }
                if (c != 'e' && c != '.') status = 1;
                break;
            case 1:
                if (c == '.')
                {
                    status = 2;
                }
                else if (c == 'e')
                {
                    positive = 1;
                    status = 3;
                }
                else if (c >= '0' && c <= '9')
                    base = base * 10 + positive * (c - '0');
                else
                {
                    cout<< "wrong format" << endl;
                    return 0.0;
                }
                break;
            case 2:
                if (c == 'e')
                {
                    positive = 1;
                    status = 3;
                }
                else if (c >= '0' && c <= '9')
                {
                    float_exp /= 10;
                    float_part = float_part + float_exp * positive * (c - '0');
                }
                else
                {
                    cout <<"wrong format" <<endl;
                    return 0.0;
                }
                break;
            case 3:
                if (c == '+') positive = 1;
                else if ( c == '-') positive = -1;
                else if (c >= '0' && c <= '9') exp = exp * 10 + positive * (c - '0');
                else
                {
                    cout <<"wrong format" <<endl;
                    return 0.0;
                }
                status = 4;
                break;
            case 4:
                if (c >= '0' && c <= '9') exp = exp * 10 + positive * (c - '0');
                else
                {
                    exp = exp * 10 + positive * (c - '0');
                    return 0.0;
                }
                break;
        }
    }
    float_part += base;
    if (exp > 0) float_part *= calculate_exp(10, exp);
    else float_part *= calculate_exp(0.1, - exp);
    return float_part;
}

float atof_lobatt(const char *s)
{
	float result = 0.0;
	bool flag_point = false;
	bool flag_e = false;
	bool started = false;
	bool positive = true;
	int d = 1;
	bool e_positive = true;
	int exp = 0;

	while(*s != '\0')
	{
		//std::cout<<"Processing: "<<s<<std::endl;
		switch(*s)
		{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				started = true;
				if(flag_e){
					exp = exp * 10 + (int)(*s) - 48.0;
				}
				else{
					if(flag_point)
					{
						result += ((int)(*s) - 48.0) / d;
						d *= 10;
					}
					else
					result = result *10 + (int)(*s) - 48.0;
				}
				break;
			case '.':
				started = true;
				if(flag_point) return 0.0;
				else flag_point = true;
				d = 10;
				break;	
			case 'e':
				if(flag_e) return 0.0;
				flag_e = true;
				started = false;
				break;
			case '-':
				if(!started && *s == '-')
				if(flag_e)
					e_positive = false;
				else
					positive = false;
				started = true;
				break;
			case '+':
				if(!started)
				{
					started = true;
					break;
				}
			default:
				return 0.0;
				
		}

		++s;
		//std::cout<<"exp = "<<exp<<std::endl;
		//std::cout<<"-->"<<result<<std::endl;
	}

	if(!positive) result *= -1;
	for(int i = exp; i > 0; i --)
	{
		result *= e_positive ? 10.0 : 1.0/10;
		//std::cout<<"-->"<<result<<std::endl;
	}
	return result;
}

int main()
{
    string strs[] = {"-3.5e-2", "", "0", "1.0", ".1", "02", ".3e3", ".3e+3", ".3e-2", "e3", "e0"};
    size_t count = 11;
    for (size_t i = 0;i < count; ++i)
    {
        const string& str = strs[i];
        float f1 = atof_david(str), f2 = atof_lobatt(str.c_str());
        float f3 = atof(str.c_str());
        if ((f1 - f3) > 0.0000001 || (f1 - f3) < -0.000001)
        {
            cout<<"david FAIL"<<endl;
            cout<<f1 - f3<<endl;
            cout<<str<<"\t"<<f3<<"\t"<<f1<<"\t"<<endl;
        }
        if ((f2 - f3) > 0.0000001 || (f2 - f3) < -0.000001)
        {
            cout<<"locatt FAIL"<<endl;
            cout<<str<<"\t"<<f3<<"\t"<<f2<<"\t"<<endl;
        }
        else
            cout<<"PASS"<<endl;
    }
}
